<%@ page pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="isperp" uri="/WEB-INF/tlds/isperp.tld"%>

<div class="header">
	<h2>
		编辑<span><font color="red">（* 为必填项）</font> </span>
	</h2>
</div>
<form:form commandName="model" cssClass="validate"
	id="nodeCategoryRelationForm" action="bpm/nodeCategoryRelation/save">
	<form:hidden path="id" />
	<form:hidden path="bpmSidName" />
	<form:hidden path="canNotWriteName" />
	<form:hidden path="requiredWriteName" />
    <form:hidden path="secrecyName" />
	<table class=" form-table">
		<tbody>
			<tr>
				<td class="td1">分类：</td>
				<td class="td2"><form:hidden path="categoryId" /> <isperp:treeSelector
						width="500" displayColumn="treenodeNamePath"
						displayValueCssStyle="width:249px" url="individuation/category/getTree?showMainForm=true"
						autocompleteUrl="individuation/category/list"
						afterSelectEmpty="clearCategory();" id="categorySelector"
						name="categoryName" displayValue="${model.categoryName }"
						afterSelected="changeCategory(ids,names,treeNode);"
						valueControlId="categoryId" displayValueCssClass="required"></isperp:treeSelector>
				</td>
			</tr>
			<tr>
				<td class="td1">状态(根据状态控制)：</td>
				<td class="td2">
				   <form:input path="canNotWriteStatus" style="width:80%"/>
				</td>
			</tr>

			<tr>
				<td class="td1">不可写字段：<input type='button' class="button" value='全选' onclick="selectAll(1)"></td>
				<td class="td2" id="canNotWriteColumnsTd"></td>
			</tr>
			
			<tr>
				<td class="td1">必填字段：<input type='button' value='全选' class="button" onclick="selectAll(2)"></td>
				<td class="td2" id="requiredWriteColumnsTd"></td>
			</tr>
			<tr>
				<td class="td1">保密字段(仅对流程发起人生效)：<input type='button' value='全选' class="button" onclick="selectAll(3)"></td>
				<td class="td2" id="secrecyColumnsTd"></td>
			</tr>
		</tbody>
	</table>
</form:form>
<script type="text/javascript">
	 $(function() {
		
	/* $.ajax({
			url : 'bpm/nodeCategoryRelation/getAllNodes',
			data : {},
			success : function(data) {
				if (data) {
					var html = "<option value='sid-CF417967-44D2-432D-9A53-3338E3957D1E'>开始<option>";
					for ( var i = 0; i < data.length; i++) {
						html += "<option  value='"+data[i].id+"'>" + data[i].name + "</option>";
					}
					$("#bpmSidId").append(html);
					var curSid='${model.bpmSidId}';
					var modelId='${model.id}';
					var categoryId='${model.categoryId}'
					if(modelId)
					{
					  changeCategory(categoryId,'','');
					  var canNotWrite='${model.canNotWrite}';
					  var ta=canNotWrite.split(",");
					  for(var i=0;i<ta.length;i++)
					  {
					     var tid=ta[i];
					     document.getElementById(tid).checked = true;
					  }
					  
					}
					$("#bpmSidId").val(curSid);
					$("#bpmSidId").trigger("chosen:updated");
				}
			}
		}); */

		var modelId = '${model.id}';
		var categoryId = '${model.categoryId}'
		if (modelId) {
			changeCategory(categoryId, '', '');
			var canNotWrite = '${model.canNotWrite}';
			var ta = canNotWrite.split(",");
			for ( var i = 0; i < ta.length; i++) {
				var tid = "cnw_"+ta[i];
				document.getElementById(tid).checked = true;
			}
			
			var requiredWrite = '${model.requiredWrite}';
			var tar = requiredWrite.split(",");
			for ( var i = 0; i < tar.length; i++) {
				var tidr = "rw_"+tar[i];
				document.getElementById(tidr).checked = true;
			}
			
			var secrecy = '${model.secrecy}';
			var tar = secrecy.split(",");
			for ( var i = 0; i < tar.length; i++) {
				var tidr = "st_"+tar[i];
				document.getElementById(tidr).checked = true;
			}

		}

	});
	
	function selectAll(type)
	{
	  if(type==1)
	  {
	     $("[name='canNotWrite']").each(function(){
		    $(this).get(0).checked=true;
		 });
	  }
	  else if(type==2)
	  {
	     $("[name='requiredWrite']").each(function(){
		    $(this).get(0).checked=true;
		 });
	  }else if(type==3)
	  {
	     $("[name='secrecy']").each(function(){
		    $(this).get(0).checked=true;
		 });
	  }
	}

	function beforeSubmit() {
		//$("#bpmSidName").val($("#bpmSidId option:selected").text());
		var ts = "";
		$("input[name='canNotWrite']:checked").each(function() {
			ts += $(this).data("name") + ",";
		});
		ts = ts.substring(0, ts.length - 1);
		$("#canNotWriteName").val(ts);
		
		var ts1 = "";
		$("input[name='requiredWrite']:checked").each(function() {
			ts1 += $(this).data("name") + ",";
		});
		ts1 = ts1.substring(0, ts1.length - 1);
		$("#requiredWriteName").val(ts1);
		
		var ts2 = "";
		$("input[name='secrecy']:checked").each(function() {
			ts2 += $(this).data("name") + ",";
		});
		ts2 = ts2.substring(0, ts2.length - 1);
		$("#secrecyName").val(ts2);

	}

	function clearCategory() {
		$("#categoryId").val("");
		$("#categorySelector").val("");
	}
	function changeCategory(ids, names, treeNode) {
		//根据分类加载扩展属性

		$.ajax({
			url : 'individuation/category/property/listByCategory',
			async : false,
			data : {
				categoryId : ids
			},
			success : function(data) {
				if (data) {
					var html = "";
					var html2="";
					var html3="";
					for ( var i = 0; i < data.length; i++) {
						html += "<span><input id='cnw_"+data[i].id+"' name='canNotWrite' type='checkbox' value='"+data[i].id+"' data-name='"+data[i].name+"'><label for='cnw_"+data[i].id+"'>" + data[i].name + "</label></span>";
					}
					for ( var i = 0; i < data.length; i++) {
						html2 += "<span><input id='rw_"+data[i].id+"' name='requiredWrite' type='checkbox' value='"+data[i].id+"' data-name='"+data[i].name+"'><label for='rw_"+data[i].id+"'>" + data[i].name + "</label></span>";
					}
					for ( var i = 0; i < data.length; i++) {
						html3 += "<span><input id='st_"+data[i].id+"' name='secrecy' type='checkbox' value='"+data[i].id+"' data-name='"+data[i].name+"'><label for='st_"+data[i].id+"'>" + data[i].name + "</label></span>";
					}
					$("#canNotWriteColumnsTd").append(html);
					$("#requiredWriteColumnsTd").append(html2);
					$("#secrecyColumnsTd").append(html3);

				}
			}
		});

	}
</script>

<isperp:buttonList>
	<button class="submit nui-mainbtn"
		onclick="beforeSubmit();isperp.submitForm();">保存</button>
</isperp:buttonList>


